package com.dk.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.dk.mapper.*;
import com.dk.pojo.Menu;
import com.dk.service.menuservice;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@org.springframework.stereotype.Service
public class menuserviceImpl implements menuservice {
    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    public List<Map<String, Object>> selectgetinfo(Integer pid) {
        List<Menu> list = menuMapper.selectgetinfo(pid);
        List<Map<String, Object>> mapList = selectegetinfotree(list, 0);
        return mapList;
    }
    public List<Map<String, Object>> selectegetinfotree(List<Menu> list,Integer id){
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = list.get(i);
            if (menu.getParentid() == id) {
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("text",menu.getText());
                map.put("url",menu.getUrl());
                map.put("nodes",selectegetinfotree(list,menu.getId()));
            }
            if (map != null){
                List<Map<String, Object>> nodes = (List<Map<String, Object>>) map.get("nodes");
                if (nodes.size() <= 0){
                    map.remove("nodes");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }

    @Override
    public List<Map<String, Object>> selectZtree() {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Map<String, Object>> mapList = selectGetZtree(list,0);
        return mapList;
    }

    public List<Map<String, Object>> selectGetZtree(List<Menu> list,Integer id){
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = list.get(i);
            if (menu.getParentid() == id) {
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                map.put("children",selectGetZtree(list,menu.getId()));
            }
            if (map != null){
                List<Map<String, Object>> nodes = (List<Map<String, Object>>) map.get("children");
                if (nodes.size() <= 0){
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;

    }


    @Override
    public List<Map<String, Object>> selectgetZtreeChecked(Integer roleId) {
        List<Menu> list = menuMapper.selectByExample(null);
        List<Integer> hids = roleMenuMapper.selectbyperid(roleId);
        List<Map<String, Object>> mapList = selectGetZtreeChecked(list,0,hids);
        return mapList;
    }

    @Override
    public Menu toUpdate(Integer id) {
        return menuMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean updateInfo(Menu menu) {
       int i = menuMapper.updateByPrimaryKeySelective(menu);
       return i>0;
    }

    @Override
    public boolean deleteInfo(Integer id) {
        int i = menuMapper.deleteByPrimaryKey(id);
        return i > 0;
    }

    @Override
    public boolean add(Menu menu) {
        int i = menuMapper.insertSelective(menu);
        return i>0;
    }

    public List<Map<String, Object>>  selectGetZtreeChecked(List<Menu> list,Integer id,List<Integer> hids){
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Map<String,Object> map = null;
            Menu menu = list.get(i);
            if (menu.getParentid() == id) {
                map = new HashMap<>();
                map.put("id",menu.getId());
                map.put("name",menu.getText());
                map.put("children",selectGetZtreeChecked(list,menu.getId(),hids));
                if (hids != null && hids.size() > 0 && hids.contains(menu.getId())){
                    map.put("checked",true);
                }
            }
            if (map != null){
                List<Map<String, Object>> nodes = (List<Map<String, Object>>) map.get("children");
                if (nodes.size() <= 0){
                    map.remove("children");
                }
                mapList.add(map);
            }
        }
        return mapList;
    }
}
